aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Blättler <blatc2@bfh.ch>2024-04-28 13:55:11 +0200
committerChristian Blättler <blatc2@bfh.ch>2024-04-28 13:55:11 +0200
commitf6aba4ba4d5cc7f8c3c73c61148f9e651c255567 (patch)
tree1a74ee5c7015616b2b6ec7659cc64132f8a78bda
parent046cd9f8760b48d86389b4b67e58d2b24966cc4f (diff)
downloadgnunet-master.tar.gz
gnunet-master.zip
JSON lib: parse and pack blinded messageHEADmaster
-rw-r--r--src/include/gnunet_json_lib.h24
-rw-r--r--src/lib/json/json_helper.c162
-rw-r--r--src/lib/json/json_pack.c40
3 files changed, 226 insertions, 0 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index 0b6b299d1..44dbf8965 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -430,6 +430,17 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
430 struct GNUNET_CRYPTO_RsaSignature **sig); 430 struct GNUNET_CRYPTO_RsaSignature **sig);
431 431
432 432
433/**
434 * Specification for parsing a blinded message.
435 *
436 * @param name name of the JSON field
437 * @param sig where to store the blinded message found under @a name
438 */
439struct GNUNET_JSON_Specification
440GNUNET_JSON_spec_blinded_message (const char *name,
441 struct GNUNET_CRYPTO_BlindedMessage **msg);
442
443
433/* ****************** Generic generator interface ******************* */ 444/* ****************** Generic generator interface ******************* */
434 445
435 446
@@ -979,6 +990,19 @@ GNUNET_JSON_pack_unblinded_signature (const char *name,
979 const struct GNUNET_CRYPTO_UnblindedSignature *sig); 990 const struct GNUNET_CRYPTO_UnblindedSignature *sig);
980 991
981 992
993/**
994 * Generate packer instruction for a JSON field of type
995 * blinded message.
996 *
997 * @param name name of the field to add to the object
998 * @param msg blinded message
999 * @return json pack specification
1000 */
1001struct GNUNET_JSON_PackSpec
1002GNUNET_JSON_pack_blinded_message (const char *name,
1003 const struct GNUNET_CRYPTO_BlindedMessage *msg);
1004
1005
982#endif 1006#endif
983 1007
984/* end of gnunet_json_lib.h */ 1008/* end of gnunet_json_lib.h */
diff --git a/src/lib/json/json_helper.c b/src/lib/json/json_helper.c
index b6965e080..cae26aef4 100644
--- a/src/lib/json/json_helper.c
+++ b/src/lib/json/json_helper.c
@@ -26,6 +26,7 @@
26 */ 26 */
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_json_lib.h" 28#include "gnunet_json_lib.h"
29#include <gnunet/gnunet_common.h>
29 30
30 31
31struct GNUNET_JSON_Specification 32struct GNUNET_JSON_Specification
@@ -42,6 +43,29 @@ GNUNET_JSON_spec_end ()
42 43
43 44
44/** 45/**
46 * Convert string value to numeric cipher value.
47 *
48 * @param cipher_s input string
49 * @return numeric cipher value
50 */
51static enum GNUNET_CRYPTO_BlindSignatureAlgorithm
52string_to_cipher (const char *cipher_s)
53{
54 if ((0 == strcasecmp (cipher_s,
55 "RSA")) ||
56 (0 == strcasecmp (cipher_s,
57 "RSA+age_restricted")))
58 return GNUNET_CRYPTO_BSA_RSA;
59 if ((0 == strcasecmp (cipher_s,
60 "CS")) ||
61 (0 == strcasecmp (cipher_s,
62 "CS+age_restricted")))
63 return GNUNET_CRYPTO_BSA_CS;
64 return GNUNET_CRYPTO_BSA_INVALID;
65}
66
67
68/**
45 * Parse given JSON object to fixed size data 69 * Parse given JSON object to fixed size data
46 * 70 *
47 * @param cls closure, NULL 71 * @param cls closure, NULL
@@ -1180,4 +1204,142 @@ GNUNET_JSON_spec_boolean (const char *name,
1180} 1204}
1181 1205
1182 1206
1207/**
1208 * Parse given JSON object to a blinded message.
1209 *
1210 * @param cls closure, NULL
1211 * @param root the json object representing data
1212 * @param[out] spec where to write the data
1213 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
1214 */
1215static enum GNUNET_GenericReturnValue
1216parse_blinded_message (void *cls,
1217 json_t *root,
1218 struct GNUNET_JSON_Specification *spec)
1219{
1220 struct GNUNET_CRYPTO_BlindedMessage **target = spec->ptr;
1221 struct GNUNET_CRYPTO_BlindedMessage *blinded_message;
1222 const char *cipher;
1223 struct GNUNET_JSON_Specification dspec[] = {
1224 GNUNET_JSON_spec_string ("cipher",
1225 &cipher),
1226 GNUNET_JSON_spec_end ()
1227 };
1228 const char *emsg;
1229 unsigned int eline;
1230
1231 (void) cls;
1232 if (GNUNET_OK !=
1233 GNUNET_JSON_parse (root,
1234 dspec,
1235 &emsg,
1236 &eline))
1237 {
1238 GNUNET_break_op (0);
1239 return GNUNET_SYSERR;
1240 }
1241 blinded_message = GNUNET_new (struct GNUNET_CRYPTO_BlindedMessage);
1242 blinded_message->rc = 1;
1243 blinded_message->cipher = string_to_cipher (cipher);
1244 switch (blinded_message->cipher)
1245 {
1246 case GNUNET_CRYPTO_BSA_INVALID:
1247 break;
1248 case GNUNET_CRYPTO_BSA_RSA:
1249 {
1250 struct GNUNET_JSON_Specification ispec[] = {
1251 GNUNET_JSON_spec_varsize (
1252 "rsa_blinded_planchet",
1253 &blinded_message->details.rsa_blinded_message.blinded_msg,
1254 &blinded_message->details.rsa_blinded_message.blinded_msg_size),
1255 GNUNET_JSON_spec_end ()
1256 };
1257
1258 if (GNUNET_OK !=
1259 GNUNET_JSON_parse (root,
1260 ispec,
1261 &emsg,
1262 &eline))
1263 {
1264 GNUNET_break_op (0);
1265 GNUNET_free (blinded_message);
1266 return GNUNET_SYSERR;
1267 }
1268 *target = blinded_message;
1269 return GNUNET_OK;
1270 }
1271 case GNUNET_CRYPTO_BSA_CS:
1272 {
1273 struct GNUNET_JSON_Specification ispec[] = {
1274 GNUNET_JSON_spec_fixed_auto (
1275 "cs_nonce",
1276 &blinded_message->details.cs_blinded_message.nonce),
1277 GNUNET_JSON_spec_fixed_auto (
1278 "cs_blinded_c0",
1279 &blinded_message->details.cs_blinded_message.c[0]),
1280 GNUNET_JSON_spec_fixed_auto (
1281 "cs_blinded_c1",
1282 &blinded_message->details.cs_blinded_message.c[1]),
1283 GNUNET_JSON_spec_end ()
1284 };
1285
1286 if (GNUNET_OK !=
1287 GNUNET_JSON_parse (root,
1288 ispec,
1289 &emsg,
1290 &eline))
1291 {
1292 GNUNET_break_op (0);
1293 GNUNET_free (blinded_message);
1294 return GNUNET_SYSERR;
1295 }
1296 *target = blinded_message;
1297 return GNUNET_OK;
1298 }
1299 }
1300 GNUNET_break_op (0);
1301 GNUNET_free (blinded_message);
1302 return GNUNET_SYSERR;
1303}
1304
1305/**
1306 * Cleanup data left from parsing blinded message.
1307 *
1308 * @param cls closure, NULL
1309 * @param[out] spec where to free the data
1310 */
1311static void
1312clean_blinded_message (void *cls,
1313 struct GNUNET_JSON_Specification *spec)
1314{
1315 struct GNUNET_CRYPTO_BlindedMessage **blinded_message = spec->ptr;
1316
1317 (void) cls;
1318 if (NULL != blinded_message)
1319 {
1320 GNUNET_CRYPTO_blinded_message_decref (*blinded_message);
1321 *blinded_message = NULL;
1322 }
1323}
1324
1325
1326struct GNUNET_JSON_Specification
1327GNUNET_JSON_spec_blinded_message (const char *name,
1328 struct GNUNET_CRYPTO_BlindedMessage **msg)
1329{
1330 struct GNUNET_JSON_Specification ret = {
1331 .parser = &parse_blinded_message,
1332 .cleaner = &clean_blinded_message,
1333 .cls = NULL,
1334 .field = name,
1335 .ptr = msg,
1336 .ptr_size = 0,
1337 .size_ptr = NULL
1338 };
1339
1340 *msg = NULL;
1341 return ret;
1342}
1343
1344
1183/* end of json_helper.c */ 1345/* end of json_helper.c */
diff --git a/src/lib/json/json_pack.c b/src/lib/json/json_pack.c
index f7c8a0b86..7b41a967e 100644
--- a/src/lib/json/json_pack.c
+++ b/src/lib/json/json_pack.c
@@ -400,4 +400,44 @@ GNUNET_JSON_pack_unblinded_signature (const char *name,
400} 400}
401 401
402 402
403struct GNUNET_JSON_PackSpec
404GNUNET_JSON_pack_blinded_message (const char *name,
405 const struct GNUNET_CRYPTO_BlindedMessage *msg)
406{
407 struct GNUNET_JSON_PackSpec ps = {
408 .field_name = name,
409 };
410
411 switch (msg->cipher)
412 {
413 case GNUNET_CRYPTO_BSA_INVALID:
414 break;
415 case GNUNET_CRYPTO_BSA_RSA:
416 ps.object = GNUNET_JSON_PACK (
417 GNUNET_JSON_pack_string ("cipher",
418 "RSA"),
419 GNUNET_JSON_pack_data_varsize (
420 "rsa_blinded_planchet",
421 msg->details.rsa_blinded_message.blinded_msg,
422 msg->details.rsa_blinded_message.blinded_msg_size));
423 return ps;
424 case GNUNET_CRYPTO_BSA_CS:
425 ps.object = GNUNET_JSON_PACK (
426 GNUNET_JSON_pack_string ("cipher",
427 "CS"),
428 GNUNET_JSON_pack_data_auto (
429 "cs_nonce",
430 &msg->details.cs_blinded_message.nonce),
431 GNUNET_JSON_pack_data_auto (
432 "cs_blinded_c0",
433 &msg->details.cs_blinded_message.c[0]),
434 GNUNET_JSON_pack_data_auto (
435 "cs_blinded_c1",
436 &msg->details.cs_blinded_message.c[1]));
437 return ps;
438 }
439 GNUNET_assert (0);
440 return ps;
441}
442
403/* end of json_pack.c */ 443/* end of json_pack.c */